import 'package:color_dart/hex_color.dart';
import 'package:color_dart/rgba_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/components/a_button/index.dart';
import 'package:flutter_project/core/util/object_util.dart';
import 'package:flutter_project/core/util/regex_util.dart';
import 'package:flutter_project/custom/appbar/asu_appbar.dart';
import 'package:flutter_project/service/user_dao.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  static Map phone = {"value": null, "verify": true};
  static Map password = {"value": null, "verify": true};
  static Map name = {"value": null, "verify": true};
  var sex = 1;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  Widget _buildRegisterTextForm() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        /// 输入手机号
        Container(
          height: 55.h,
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(color: rgba(242, 242, 242, 1), width: 1)
              )
          ),
          child: TextField(
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
                counterText: "",
                border: InputBorder.none,
                hintText: '请输入手机号',
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                )),
            onChanged: (e) {
              setState(() {
                phone['value'] = e;
                phone['verify'] = RegexUtil.isMobileSimple(e);
              });
            },
          ),
        ),

        /// 密码
        Container(
          height: 55.h,
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(color: rgba(242, 242, 242, 1), width: 1)
              )
          ),
          child: Row(
            children: <Widget>[
              Flexible(
                child: TextField(
                  obscureText: true,
                  decoration: InputDecoration(
                      counterText: '',
                      border: InputBorder.none,
                      hintText: '请输入密码',
                      hintStyle: TextStyle(
                        fontSize: 14.sp,
                      )),
                  onChanged: (e) {
                    setState(() {
                      password['value'] = e;
                      password['verify'] = !ObjectUtil.isEmpty(e);
                    });
                  },
                ),
              ),
            ],
          ),
        ),

        /// 姓名
        Container(
          height: 55.h,
          decoration: BoxDecoration(
              border: Border(
                  bottom: BorderSide(color: rgba(242, 242, 242, 1), width: 1)
              )
          ),
          child: TextField(
            keyboardType: TextInputType.text,
            decoration: InputDecoration(
                counterText: "",
                border: InputBorder.none,
                hintText: '请输入姓名',
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                )),
            onChanged: (e) {
              setState(() {
                name['value'] = e;
                name['verify'] = !ObjectUtil.isEmpty(e);
              });
            },
          ),
        ),

        Row(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: 100.w,
              child: Text(
                  "性别"
              ),
            ),
            Expanded(
                flex: 1,
                child: RadioListTile(
                  value: 1,
                  onChanged: (v) {
                    setState(() {
                      this.sex = v;
                    });
                  },
                  groupValue: this.sex,
                  title: Text("男"),
                  selected: this.sex == 1,
                )
            ),
            Expanded(
                flex: 1,
                child: RadioListTile(
                  value: 2,
                  onChanged: (v) {
                    setState(() {
                      this.sex = v;
                    });
                  },
                  groupValue: this.sex,
                  title: Text("女"),
                  selected: this.sex == 2,
                )
            ),
          ],
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AsuAppbar(title: '注册', back: true),
      body: Container(
        color: hex('#ffffff'),
        height: double.infinity,
        padding: EdgeInsets.only(left: 35.w, right: 35.w, top: 80.h),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            _buildRegisterTextForm(),

            /// 注册
            Container(
              margin: EdgeInsets.only(top: 20.h),
              child: AButton.normal(
                  width: double.infinity,
                  child: Text('注册'),
                  bgColor: hex('#0096FF'),
                  color: hex('#fff'),
                  onPressed: () => register()),
            ),
          ],
        ),
      ),
    );
  }

  register() async {
    if (!phone['verify'] || phone['value'] == null) {
      return Fluttertoast.showToast(msg: '请检查手机号');
    }
    if (!password['verify'] || password['value'] == null) {
      return Fluttertoast.showToast(msg: '请输入密码');
    }
    if (!name['verify'] || name['value'] == null) {
      return Fluttertoast.showToast(msg: '请输入姓名');
    }

    UserDao.register(
        phone: phone['value'],
        password: password['value'],
        name: name['value'],
        gender: sex
    ).then((data) {
      phone = {"value": null, "verify": true};
      password = {"value": null, "verify": true};
      name = {"value": null, "verify": true};
      sex = 1;
      Fluttertoast.showToast(msg: '注册成功，2秒后跳转到登录页面');
      // 2秒后跳转到登录页面
      Future.delayed(Duration(seconds: 2), () {
        Navigator.pushReplacementNamed(context, '/login');
      });
    }).catchError((err) {
      Fluttertoast.showToast(msg: err.message);
    });
  }
}
